﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows;
#if (!WINRT)
using System.Windows.Controls;
using System.Windows.Interactivity;
#else
using nRoute.Behaviors.Interactivity;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using System.Windows.Interactivity;
using Windows.UI.Xaml.Controls;
#endif

namespace nRoute.Behaviors
{
    public class NullValueDisableBehavior
#if (WINRT)
        : Behavior<FrameworkElement>
#else
        : Behavior<UIElement>
#endif
    {
        private const double DISABLE_OPACITY = 0.4d;
        private const double ENABLE_OPACITY = 1d;

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(object), typeof(NullValueDisableBehavior),
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, new PropertyChangedCallback(OnValueChanged)));

        private bool _negate;
        private double _disableOpacity = DISABLE_OPACITY;

#region Properties

        [Category("Common Properties")]
//#if WP7
//        [TypeConverter(typeof(nRoute.Components.TypeConverters.ConvertFromStringConverter))]
//#endif
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Object Value
        {
            get { return GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

#if (WINRT)

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding ValueBinding
        {
            get { return GetBinding(ValueProperty); }
            set { SetBinding<Object>(ValueProperty, value); }
        }

#endif

        [Category("Common Properties")]
        public bool Negate
        {
            get { return _negate; }
            set { _negate = value; }
        }

        [Category("Common Properties")]
        public double DisableOpacity
        {
            get { return _disableOpacity; }
            set { _disableOpacity = value; }
        }

#endregion

#region Trigger Related

        protected override void OnAttached()
        {
            base.OnAttached();
            UpdateIsDisabled(Value);
        }

#endregion

#region Handlers

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((NullValueDisableBehavior)d).UpdateIsDisabled(e.NewValue);
        }

        private void UpdateIsDisabled(object value)
        {
            if (this.AssociatedObject == null) return;

            var _isDisabled = (value == null);
            if (Negate) _isDisabled = !_isDisabled;

#if (SILVERLIGHT || WINRT)
            if (AssociatedObject is Control)
            {
                (AssociatedObject as Control).IsEnabled = !_isDisabled;
            }
            else
            {
                this.AssociatedObject.Opacity = !_isDisabled ? ENABLE_OPACITY : DisableOpacity;
                this.AssociatedObject.IsHitTestVisible = !_isDisabled;
            }
#else
            AssociatedObject.IsEnabled = !_isDisabled;
#endif
        }

#endregion

    }
}
